//
//  MyCollectionViewController.m
//  LimitFree
//
//  Created by lijinghua on 16/3/4.
//  Copyright © 2016年 lijinghua. All rights reserved.
//

#import "MyCollectionViewController.h"
#import "MyCollectionViewCell.h"
#import "AppDetailModel.h"
#import "AppDetailViewController.h"

@interface MyCollectionViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property(nonatomic)NSArray *dataSource;
@property(nonatomic)UICollectionView *collectionView;

@end

@implementation MyCollectionViewController

- (id)init{
    if (self = [super init]) {
        self.navigationStyle = LFNavigationBackStyle;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //从数据库中读取收藏的应用
    self.dataSource = [[AppDataBaseManager sharedInstance] allApp];
    //把数据显示在界面上
    [self.view addSubview:self.collectionView];
}

- (UICollectionView*)collectionView{
    if (_collectionView == nil) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
        flowLayout.itemSize = CGSizeMake(60, 80);
        flowLayout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-64) collectionViewLayout:flowLayout];
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.dataSource = self;
        _collectionView.delegate   = self;
        
        //注册使用的cell
        UINib *nib = [UINib nibWithNibName:@"MyCollectionViewCell" bundle:nil];
        [_collectionView registerNib:nib forCellWithReuseIdentifier:@"cellId"];
    }
    return _collectionView;
}

#pragma mark - UICollectionView协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellId" forIndexPath:indexPath];
    AppDetailModel *model = self.dataSource[indexPath.row];
    [cell updateWithModel:model];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    AppDetailModel *model = self.dataSource[indexPath.row];
    AppDetailViewController *appDetailVC = [[AppDetailViewController alloc]init];
    appDetailVC.applicationId = model.applicationId;
    [self.navigationController pushViewController:appDetailVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
